package com.czk.model.proxy.dynamincproxy.practice4;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/7/27 18:03
 */
public class CachedDataServiceProxy implements DataService {
    private static Map<String,String> cacheMap=new ConcurrentHashMap<>();
    private DataService dataService;

    public CachedDataServiceProxy(DataService dataService) {
        this.dataService = dataService;
    }

    @Override
    public String getData() {
        String cache = cacheMap.get("cache");
        if (cache == null){
            String data = dataService.getData();
            cacheMap.put("cache",data);
            return data;
        }
        return cache;
    }
}
